#!/usr/bin/perl -wT

# cord@debian.org

require 5.001;
use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use Data::Dumper;

require '/org/lists.debian.org/archive-spam-removals/etc/review.conf';
die ("$0: /org/lists.debian.org/archive-spam-removals/etc/review.conf incorrect: $!") unless $SpamReviewConf::max_reviews;

my $cachedir='/org/lists.debian.org/archive-spam-removals/review';
my $reviewlog='/org/lists.debian.org/spam-log/spam-review.log';

umask 002;

my $q = new CGI;

print $q->header;
my $remote_user = remote_user();
my %reviewed;
my %count;

open (HEADER, "/org/lists.debian.org/html/header.in") or 
  die ("$0: can't open /org/lists.debian.org/html/header.in: $!\n");
while (<HEADER>) {
  s/u_TITLE_u/Listarchive Review/;
  print "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\"></head>\n" if m#^</head>$#;
  print;
}
close (HEADER);

print "<H2>Listarchive Review</h2>\n";

printf "<p>Last update at %s\n", scalar localtime((stat("$cachedir/cache"))[9]);

print "<hr noshade width=\"100%\" size=\"1\">\n";

if (-e "$cachedir/cache") {
  open (CACHE, "$cachedir/cache") or die ("$0: can't open $cachedir/cache for reading: $!\n");
  while (<CACHE>) {
    chomp;
    my ($post, $count, $msgid, $ham, $spam, $inappropriate) = split (/:/);
    next unless $msgid;
    next if $count < $SpamReviewConf::num_nominations;

    my $hamcount = split(',', $ham);
    my $spamcount = split(',', $spam);
    my $inappropriatecount = split(',', $inappropriate);
    next if ($inappropriatecount >= ($hamcount*$SpamReviewConf::minority_factor+$spamcount*$SpamReviewConf::minority_factor+$SpamReviewConf::num_ratings));
    next if ($hamcount >= ($inappropriatecount*$SpamReviewConf::minority_factor+$spamcount*$SpamReviewConf::minority_factor+$SpamReviewConf::num_ratings));
    next if ($spamcount >= ($hamcount*$SpamReviewConf::minority_factor+$inappropriatecount*$SpamReviewConf::minority_factor+$SpamReviewConf::num_ratings));

    my $list = (split(m#\/#, $post))[0];
    $count{$list}++;
    my @reviewers = split(',', join(',', $ham, $spam, $inappropriate) );
    my $reviewer;
    foreach $reviewer (@reviewers) {
      $reviewed{$list}{$reviewer}++;
    }
  }
  close (CACHE);
}

if (-e $reviewlog) {
  open (REVIEWLOG, $reviewlog) or die ("$0: can't open $reviewlog for reading: $!\n");
  while (<REVIEWLOG>) {
    if (m#^time: (\d+); addr: (\d+\.\d+\.\d+\.\d+); list: ([a-z0-9-]+), date: (\d+/\d*), msg: (msg\d+.html), rating: (Unsure|Spam|Ham|Inappropriate), reviewer: (\S+)$#) {
      $reviewed{$3}{$7}++;
      $reviewed{"$3/$4/$5"}++;
    }
    if ($reviewed{"$3/$4/$5"} >= $SpamReviewConf::max_reviews) {
      $count{$3}--;
      next;
    }
  }
  close (REVIEWLOG);
}

my $list;
foreach $list (sort {($count{$b}-($reviewed{$b}{$remote_user}?$reviewed{$b}{$remote_user}:0)) <=> 
		     ($count{$a}-($reviewed{$a}{$remote_user}?$reviewed{$a}{$remote_user}:0))} keys %count) {
  if (($count{$list}-($reviewed{$list}{$remote_user}?$reviewed{$list}{$remote_user}:0)) > 0) {
    print "<a href=\"/cgi-bin/review/review2.pl?$list\">$list</a> (" . ($reviewed{$list}{$remote_user} ? $reviewed{$list}{$remote_user} : '0') . " of $count{$list} reviewed)<br>\n";
  }
}

open (FOOTER, "/org/lists.debian.org/html/footer.in") or 
  die ("$0: can't open /org/lists.debian.org/html/footer.in: $!\n");
while (<FOOTER>) {
  print;
}
close (FOOTER);


exit 0;

